﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class StatementDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IStatementDetailDA statementDetailDA = DataAccess.CreateStatementDetailDA();

        public StatementDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public StatementDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public StatementDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteStatementDetail(string StatementID, List<string> IDS)
        {
            string temp = string.Empty;
            try
            {
                List<StatementDetailInfo> statementInfoInfos = new List<StatementDetailInfo>();
                StatementDetailInfo statementInfoInfo = null;
                foreach (string id in IDS)
                {
                    statementInfoInfo = statementDetailDA.GetStatementDetailByID(StatementID, id, this.dataBaseID[0]);
                    statementInfoInfos.Add(statementInfoInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (statementDetailDA.DeleteStatementDetail(StatementID, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<StatementDetailInfo>(statementInfoInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "STATEMENT_DETAIL", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    return temp.Substring(0, temp.Length - 1);
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return temp;
        }

        public DataSet GetAllStatementDetailInfo()
        {
            return statementDetailDA.GetAllStatementDetail(this.dataBaseID[0]);
        }

        public StatementDetailInfo GetStatementDetailInfoByID(string statementID, string LineID)
        {
            return statementDetailDA.GetStatementDetailByID(statementID, LineID, this.dataBaseID[0]);
        }

        public string InsertStatementDetail(StatementDetailInfo obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string StatementDetail = string.Empty;
            try
            {
                if (statementDetailDA.InsertStatementDetail(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    StatementDetail = obj.LineID.ToString();
                    new AuditDataBLL().InsertAuditData<StatementDetailInfo>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "STATEMENT_DETAIL", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return StatementDetail;
        }

        public string UpdateStatementDetail(StatementDetailInfo obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string StatementDetailID = string.Empty;
            try
            {
                StatementDetailInfo oldstatementInfoInfo = statementDetailDA.GetStatementDetailByID(dataBase, tran, obj.StatementID, obj.LineID.ToString(), this.dataBaseID[0]);
                if (statementDetailDA.UpdateStatementDetail(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    StatementDetailID = obj.LineID.ToString();
                    new AuditDataBLL().InsertAuditData<StatementDetailInfo>(dataBase, tran, oldstatementInfoInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "STATEMENT_DETAIL", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return StatementDetailID;
        }

        public string UpdateStatementDetailReceived(StatementDetailInfo obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string StatementDetailID = string.Empty;
            try
            {
                if (statementDetailDA.UpdateStatementDetailReceived(obj, this.dataBaseID[0]) > 0)
                {
                    StatementDetailID = obj.LineID.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return StatementDetailID;
        }
    }
}

